<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="form"
      :empty-text="emptyText"
      style="width: 100%;margin-top:10px; overflow:auto"
      border
    >
      <el-table-column min-width="160" align="center" prop="originalName" label="文档名" />
      <el-table-column align="center" prop="fileSize" label="文件大小(KB)" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="download(scope.row)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { tableHeight2 } from '@/utils/tableHeight'
import Pagination from '@/components/Pagination'
// import { getFileList, downloadFile } from '@/api/courseUploads'
export default {
  components: {
    Pagination
  },
  provide () {
    return {
      getList: this.getList
    }
  },
  mixins: [tableHeight2],
  props: {
    courseId: {
      type: String,
      required: true
    },
    filetype: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      emptyText: "该记录目前没有附件",
      id: '',
      path: '',
      fileName: '',
      code: '',
      listLoading: false,
      form: {
        id: '',
        fileSize: '',
        relativePath: '',
        fileName: ''
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.screenLoading = true
      this.form = []
      

      getFileList({ courseId: this.courseId, filetype: this.filetype }).then(response => {
        this.form = response.data
      }).catch(function () {
      })
    },
    download (row) {
      downloadFile({ id: row.id }).then(response => {
        var fileName = row.originalName
        const contentDisposition = response.headers['content-disposition']
        if (contentDisposition) {
          fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1], 'UTF-8')
        }
        const blob = new Blob([response.data], {
          type: `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` // word文档为msword,pdf文档为pdf
        })
        const objectUrl = URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = objectUrl
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        // 释放内存
        window.URL.revokeObjectURL(link.href)
      })
    }
  }
}
</script>